﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace 责任链模式
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //定义几个采购需求
            PurchaseRequest request1 = new PurchaseRequest("办公文具", 6500);
            PurchaseRequest request2 = new PurchaseRequest("电脑", 12500);
            PurchaseRequest request3 = new PurchaseRequest("办公桌椅", 85000);

            Approver manager = new Manager("张三");
            Approver vicePresident = new VicePresident("李四");
            Approver president = new President("王五");

            //设置责任链
            manager.NextApprover = vicePresident;
            vicePresident.NextApprover = president;

            //处理采购请求
            manager.ProcessRequest(request1);
            manager.ProcessRequest(request2);
            manager.ProcessRequest(request3);

        }
    }

    //采购需求
    class PurchaseRequest
    {
        public string ProductName { get; set; } //产品名称
        public double ProductAmount { get; set; } //采购金额

        public PurchaseRequest(string productName, double productAmount)
        {
            ProductName = productName;
            ProductAmount = productAmount;
        }
    }

    //审批人的抽象类
    abstract class Approver
    {
        //下一级审批人
        public Approver NextApprover { get; set; }
        public string Name { get; set; }
        protected Approver(string name)
        {
            Name = name;
        }

        //处理购买需求的审批
        public abstract void ProcessRequest(PurchaseRequest purchase);
    }

    //审批人1  经理
    class Manager : Approver
    {
        public Manager(string name) : base(name)
        {
        }

        public override void ProcessRequest(PurchaseRequest purchaseRequest)
        {
            if (purchaseRequest.ProductAmount < 10000)
            {
                Console.WriteLine($"{Name}对采购需求{purchaseRequest.ProductName}进行了审批！");
            }
            else
            {
                NextApprover.ProcessRequest(purchaseRequest);
            }
        }
    }

    //审批人2  副总
    class VicePresident : Approver
    {
        public VicePresident(string name) : base(name)
        {
        }

        public override void ProcessRequest(PurchaseRequest purchaseRequest)
        {
            if (purchaseRequest.ProductAmount < 25000)
            {
                Console.WriteLine($"{Name}对采购需求{purchaseRequest.ProductName}进行了审批！");
            }
            else
            {
                NextApprover.ProcessRequest(purchaseRequest);
            }
        }
    }

    //审批人3  总经理
    class President : Approver
    {
        public President(string name) : base(name)
        {
        }

        public override void ProcessRequest(PurchaseRequest purchaseRequest)
        {
            if (purchaseRequest.ProductAmount < 100000)
            {
                Console.WriteLine($"{Name}对采购需求{purchaseRequest.ProductName}进行了审批！");
            }
            else
            {
                Console.WriteLine("需要组织股东会议协商！");
            }
        }
    }
}
